
//添加购物车折扣计算器
var result2; //保存点击运算符之前输入框中的数值
var operator2; //保存运算符
var isPressEqualsKey2 = false; //记录是否按下”=“键
//数字键事件
function connectionDigital2(control2)
{
var txt2 = document.getElementById('txtScream2');
if(isPressEqualsKey2)
{ 
txt2.value = ""; //已进行过计算，则清空数值输入框重新开始
isPressEqualsKey2 = false;
}
//数值输入已经存在小数点,则不允许再输入小数点
if(txt2.value.indexOf('.') > -1 && control2.value == '.')
return false;
txt2.value += control2.value; //将控件值赋给数值输入框中
}
//退格键事件
function backspace2()
{
var txt2 = document.getElementById('txtScream2');
txt2.value = txt2.value.substring(0,txt2.value.length - 1);
}
//ce键事件：清空数字输入框
function clearAll2()
{
document.getElementById('txtScream2').value = "";
result2 = "";
operator2 = "";
}
// +、-、*、/ 事件
function calculation2(control2)
{
//将运算符保存入全局变量中
operator2 = control2.value; 
var txt2 = document.getElementById('txtScream2');
if(txt2.value == "")return false; //数值输入框中没有数字，则不能输入运算符
//将数值输入框中的值保存到计算表达式中
result2 = txt2.value; 
//清空输入框，以待输入操作值
txt2.value = ""; 
}
//计算结果
function getResult2()
{
var opValue2;
//计算表达式中存在运算符
var sourseValue2 = parseFloat(result2);
var txt2 = document.getElementById('txtScream2');
if(operator2 == '*')
opValue2 = sourseValue2 * parseFloat(txt2.value);
else if(operator2 == '/')
opValue2 = sourseValue2 / parseFloat(txt2.value);
else if(operator2 == '+')
opValue2 = sourseValue2 + parseFloat(txt2.value);
else if(operator2 == '-')
opValue2 = sourseValue2 - parseFloat(txt2.value);
txt2.value = opValue2;
isPressEqualsKey2 = true;
result2 = "";
opValue2 = "";
}





function hideheader(){

  $('#demoheader').hide();
  $('#demoheader-off').addClass("active");

}

function showheader(){
 $('#demoheader').show();
 $('#demoheader-off').removeClass("active");


}
/*点击购物车 商品弹出  选择数量弹框*/
function showEditPopup(goods_id,obj){

  $('#edit_cart_item_popup').show();
  $(obj).parent().addClass('active');

  document.getElementById("webpos_overlay").style.display="block";   

}


function fenyejia(){

	var fynum = document.getElementById('fenyenum').innerHTML;
	
	
	
	var fynum = parseInt(fynum)+1;
	
	
	var zpage = document.getElementById('zongpage').innerHTML;
	
	var zpage = parseInt(zpage);
	
	if(fynum > zpage){
		
		$("#fenyenum").text(zpage);
	}else{
		
		$("#fenyenum").text(fynum);
	}
	
	if(document.getElementById("catid")){
		
	var cid = document.getElementById('catid').innerHTML;
	
	}
	
	if(fynum > zpage){}else{
	$.ajax({ 
					
						url: "index.php?act=fenye",
						cache:false,
						data: {'fynum':fynum,'cid':cid}, 
						dataType: 'json', 
						type: 'post', 
						
						 beforeSend:function(){
             
							   $('#ajaxload').show();
							   $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
							},
							
						success: function(data){
								
							$('#ajaxload').hide();
							$("#ajaxload img").remove();
								
							document.getElementById('product_content').innerHTML = data.content;
							//document.getElementById('fenyenum').innerHTML = data.fynum;
							
							$("#menu2").hide();
							$("#webpos_overlay").hide();
								
						}
					});
	}				
	
}

function fenyejian(){

	var fynum = document.getElementById('fenyenum').innerHTML;
	
	var fynum = parseInt(fynum)-1;
	
	if(fynum < 1){ var fynum = 1; }
	
	
	
	$("#fenyenum").text(fynum); 
	
	if(document.getElementById("catid")){
		
	var cid = document.getElementById('catid').innerHTML;
	
	}
	
	
	$.ajax({ 
					
						url: "index.php?act=fenye",
						cache:false,
						data: {'fynum':fynum,'cid':cid}, 
						dataType: 'json', 
						type: 'post', 
						
						beforeSend:function(){
             
							   $('#ajaxload').show();
							   $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
							},
							
						success: function(data){
							
							$('#ajaxload').hide();
							$("#ajaxload img").remove();
											
							document.getElementById('product_content').innerHTML = data.content;
							//document.getElementById('fenyenum').innerHTML = data.fynum;
							$("#menu2").hide();
							$("#webpos_overlay").hide();
								
						}
					});
	
}



function deleteProduct(goods_id){

	
	
	$.ajax({ 
					
						url: "index.php?act=deleteProduct",
						cache:false,
						data: {'goods_id':goods_id}, 
						dataType: 'json', 
						type: 'post', 
						beforeSend:function(){
             
							   $('#ajaxload').show();
							   $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
							},
							
						success: function(data){
							
							$('#ajaxload').hide();
							$("#ajaxload img").remove();
											
							//document.getElementById('total_number_item').innerHTML = data.cnum;	
							//document.getElementById('zongji').innerHTML = data.zongji;		
							//document.getElementById('webpos_subtotal_button').innerHTML = data.zongji;	
							
							document.getElementById('qiehuan').innerHTML = data.content;
							//$('#dele').hide();
							//$('#webpos_cart').show(); 
							
               if($('#webpos_cart > div').length===0){

					
                   $('#footer_right_overlay').removeClass('hide');
                   $('#footer_right_overlay').css('width','100%');
                    $('#bt_hold_order').hide();

                    $('#bt_checkout').css('width','96%');
               }
							

							
						}
					});
          

       /*if($('#webpos_cart').find('div').length==0){
           alert('ok');
         $('#footer_right_overlay').removeClass('hide');

       }*/



	
}


function addCustomerToCart(uid){
	
	
	
	
	$.ajax({ 
					
						url: "index.php?act=addCustomerToCart",
						cache:false,
						data: {'uid':uid}, 
						dataType: 'json', 
						type: 'post', 
						success: function(data){
										
										
							document.getElementById('add-customer').innerHTML = data.content;
							
							document.getElementById('create-customer').innerHTML = data.content2;

							//用户信息改变
							
							$('#show-customer-orders').show();
							$('#remove-customer').show();
							$('#popup-customer').hide();
								
						}
					});
					
					
	
	
}



function hideCustomPriceTab(){
  
    $('#edit_cart_item_popup .edit_cart_item_popup').css('height','auto');
    $('.custom_price_tab').css('right','-100%');

}

function transactionInputAmountOnfocus(){

  $('#transaction_note_wapper').show();

}


function shippingHeaderClick(obj){
  
  $(obj).next().toggle();

}

function showEditAddress(){
	
	showAddCustomer();
  //$('#bg-shipping-address').show();
  //$("#shipping-address").show();


}

function cancelEditAddress(){
  
 $("#shipping-address").hide();
 $('#bg-shipping-address').hide();
}



function saveShippingAddress(){
    
	
	$.ajax({
		cache: true,
		type: "POST",
		url:"index.php?act=saveShipping",
		data:$('#form-shipping-address').serialize(),// 你的formid
		async: false,
		error: function(request) {
			
		},
		success: function(data) {
		
			$("#shipping-address").hide();
			$('#bg-shipping-address').hide();
  
		}
	});
	
	
	
	
	
 
  //alert('保存中... '); 


}

function showDiscount(){
 var num= $('#webpos_cart div').length;
 
 
 
      if(num==0){
         $('#toaster').show();
         msg('danger','This feature is not available for offline mode');
      }else{ 
        $('#discount-webpos').show();
         document.getElementById("webpos_overlay").style.display="block";   
      }

      
}

function gxyonghu(uid){
	
	$.ajax({
		cache: true,
		type: "POST",
		url:"index.php?act=gxyonghu&uid="+uid,
		data:$('#form-create-customer').serialize(),// 你的formid
		async: false,
		error: function(request) {
			
		},
		success: function(data) {
		
			if(data > 0){
					
			addCustomerToCart(data);		
					
		   $('.bg-create-customer').hide(); 
           $('#add-customer').addClass('active');
           //显示右边两个按钮
           $('#show-customer-orders').show();
           $('#remove-customer').show();
		   $("#create-customer").hide();
		   		
		   
           //改变 onclick 事件函数
            //$('#add-customer').attr("onclick","showAddCustomer()");
 
           //$('#add-customer').html(res);
		  
				//$("#form-create-customer").hide();
				//$('#bg-shipping-address').hide();
			}
		}
	});
	
}

function saveyonghu(){
    
	
	$.ajax({
		cache: true,
		type: "POST",
		url:"index.php?act=saveyonghu",
		data:$('#form-create-customer').serialize(),// 你的formid
		async: false,
		error: function(request) {
			
		},
		success: function(data) {
		
			if(data > 0){
					
			addCustomerToCart(data);		
					
		   $('.bg-create-customer').hide(); 
           $('#add-customer').addClass('active');
           //显示右边两个按钮
           $('#show-customer-orders').show();
           $('#remove-customer').show();
		   $("#create-customer").hide();
		   		
		   
           //改变 onclick 事件函数
            //$('#add-customer').attr("onclick","showAddCustomer()");
 
           //$('#add-customer').html(res);
		  
				//$("#form-create-customer").hide();
				//$('#bg-shipping-address').hide();
			}
		}
	});
	
	
	
	 //$('#create-customer').hide(); 
  
          // $('.bg-create-customer').hide(); 
          // $('#add-customer').addClass('active');
           //显示右边两个按钮
          // $('#show-customer-orders').show();
          // $('#remove-customer').show();
           //改变 onclick 事件函数
           // $('#add-customer').attr("onclick","showAddCustomer()");
 
          // $('#add-customer').html(res);
	
 
  //alert('保存中... '); 


}

function delToCart(gid){
									
					
					 					
							
					$.ajax({ 
					
						url: "index.php?act=deltocart",
						cache:false,
						data: {'gid':gid}, 
						dataType: 'json', 
						type: 'post', 
						
						beforeSend:function(){
             
							   $('#ajaxload').show();
							   $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
							},
							
						success: function(data){
							
							$('#ajaxload').hide();
							$("#ajaxload img").remove();
								
							$('#footer_right_overlay').addClass('hide');					
							//document.getElementById('total_number_item').innerHTML = data.cnum;	
							//document.getElementById('zongji').innerHTML = data.zongji;			
							
							//document.getElementById('webpos_subtotal_button').innerHTML = data.zongji;							
							//document.getElementById('grand_total_webpos').innerHTML = data.zongji;		
							//document.getElementById('success_orderGrandTotal').innerHTML = data.zongji;		
							
							document.getElementById('qiehuan').innerHTML = data.content;
							//$('#webpos_cart').show(); 
							
							//if(data.gnum >1 ){
																							
								//document.getElementById("webpos_overlay").style.display="block"; 
								
								//$('#catnum'+data.gid).text(data.gnum);
								//$('#catnum'+data.gid).show();		

							//}	

           //hjl  隐藏规格弹窗
           var id='#'+gid+'_options_popup';
           
           $(id).hide();

								
						}
					});
				
				}
				
				
				
function clearcart(){
    
	
	
	$.ajax({ 
					
						url: "index.php?act=clearcart",
						cache:false,
						data: {'num':1}, 
						dataType: 'json', 
						type: 'post', 
						
						beforeSend:function(){
             
							   $('#ajaxload').show();
							   $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
							},
							
						success: function(data){
							
						
								$('#ajaxload').hide();
							$("#ajaxload img").remove();
								
								
								//$('#webpos_cart').hide(); 
								//document.getElementById('total_number_item').innerHTML = 0;	
								//document.getElementById('zongji').innerHTML = 0;
								
								document.getElementById('qiehuan').innerHTML = data.content;
								
                //hjl  隐藏 hold 按钮  禁止按钮点击
                $('#footer_right_overlay').removeClass('hide');

                $('#bt_hold_order').hide();

                $('#bt_checkout').css('width','96%');
								
						
												
								
						}
	});
		

}



function webposPlaceOrder(){
	
	var yhprice= $('#yhprice').text();	
	
	var yhid= $('#yhid').text();	
	
	var comment= $('#order-comment-content').val();	
	
	var shipping_id= $('#s_method_ups_GND').val();
	var pay_id= $('#p_method_cp1forpos').val();
		
	$.ajax({ 
					
		url: "index.php?act=done",
		cache:false,
		data: {'yhprice':yhprice,'yhid':yhid,'comment':comment,'shipping_id':shipping_id,'pay_id':pay_id}, 
		dataType: 'json', 
		type: 'post', 
		success: function(data){
			
		
			
				
				 document.getElementById('success_orderIncrementId').innerHTML = data.order_sn;	
				 document.getElementById('success_orderGrandTotal').innerHTML = data.order_amount;	
				
				 $('#piaoju').attr('href','fpdf16/ex.php?order_id='+data.order_id); 	
				
				 $('#shipping_payment_wrapper').hide();
				 $('#checkout_loader').hide();
				 $('#order-success').removeClass('hide');
				 $('#order_info').css('width','54%');
				 
				 $('#toaster').show(); 
				$('#continue_select_product').addClass('hide');

				 //$('#toaster div').toggleClass('alert-success');
				 //$('#toaster div').toggleClass('alert-danger');
				 //$('#toaster div span:eq(2)').text("message");
				 //$('#toaster div span:last').text('Your order has been placed. You can save it when you are connected to the network again');
				 msg('success','订单生成成功');
				cz_order_list();
			
								
				
		}
	});
	
	
 

}

//开始新的订单按钮
function applyOptionsAfterPlaceOrder(){
 
  $('#total-right').css('float','left');
  $('#product-left').show();
  $('#bt_hold_order').show();
  $('#bt_checkout').show();
  $('#order_info').addClass('orderHide').css('width','0%');
  $('#shipping_payment_wrapper').show();
  $("#add-discount").show();
  $('#pos_totals .online_totals').hide();
  $('#webpos_cart').empty(); //清空购物车
  $('#webpos_cart_overlay').addClass('hide'); //hjl  关闭购物车遮罩
  $('#customer_loader').removeClass('hide');  //hjl  显示清除购物车按钮


}

/*
  信息提示函数
  type: 错误：danger   成功 :success
  content:内容
*/

function msg(type,content){

  $('#toaster').show();
	$('#toaster div').removeClass('alert-success alert-danger');
  if(type=='success'){
     $('#toaster div').addClass('alert-success');
     $('#toaster div span:eq(2)').text("message");
     $('#toaster div').removeClass('alert-danger');
  } else if(type=='danger'){
     $('#toaster div').removeClass('alert-success');
     $('#toaster div').toggleClass('alert-danger');
     $('#toaster div span:eq(2)').text("error");
  }
  $('#toaster div span:last').text(content);
  //$('#toaster').slideUp(8000);
   $('#toaster').delay(5000).hide(0);

}


function searchlist(){
				
				
					var cname = $("#search_customer").val();	
				
				
				
					
					$.ajax({ 
					
						url: "index.php?act=search_customer",
						cache:false,
						data: {'cname':cname}, 
						dataType: 'json', 
						type: 'post', 
						success: function(data){
												
								/*  $('html').toggleClass('mm-opened mm-blocking mm-background mm-opening'); 
								 $('#menu2').toggleClass('mm-current mm-opened');
			   
								document.getElementById("webpos_overlay").style.display="none"; */
								
								
								document.getElementById('customer_list').innerHTML = data.content;
								
						}
					});
				
				}
				

/*  function loginWebpos(){
   $('#login-webpos').toggleClass('hide');
   $('#login_overlay').toggleClass('hide');
   $('#toaster a').removeClass('alert-success');
   msg('success','登录成功');


} */

function clickGeneral(){
  $('#general,#account').removeClass('active');
  $('#general').toggleClass('active');
  $('#content_general').show();

  $('#content_account').hide();



}


function  clickAccount(){
  $('#general,#account').removeClass('active');
  $('#account').toggleClass('active');
  $('#content_general').hide();

  $('#content_account').show();

}


function selectTill(){
   $('#till_area').css('top','-100vh');
   document.getElementById("webpos_fixed_overlay").style.display="none"; 

}

function paymentHeaderClick(){

   $('#shipping_area .hidding').hide();
}

   function saveCart(){
     
      $('#total-right').css('float','left');
      
      /*if(document.getElementById("product-left").style.display=='none'){
        
        document.getElementById("product-left").style.display='block';
      }else{

         
      }*/
     
    document.getElementById("product-left").style.display="none";

      
     var obj=document.getElementById("order_info");
     var classVal = obj.getAttribute("class");
    
      classVal = classVal.replace("orderHide","");
     
      obj.setAttribute("class",classVal );
      obj.style.width="54%";
     
     $('#pos_totals .online_totals').show();
     
      document.getElementById("bt_hold_order").style.display="none";
    
      document.getElementById("bt_checkout").style.display="none";
     document.getElementById("add-discount").style.display="none";



     $('#continue_select_product').removeClass('hide');
     $('#customer_loader').addClass('hide');
     $('#grand_total_wp_show').show();   //hjl 显示总计

    //hjl  打开购物车遮罩  清除删除按钮

    $('#webpos_cart_overlay').removeClass('hide');
    $('#webpos_cart > div').find('.delete ').hide();
   


     
   
   }




   /*reposts 下拉选择效果*/
  
   function loadReport(val){
      if(val=='x_report'){
         $('#op3div').hide();
        $('#op4div').hide();
        $('#op2div').show();
        get_any_report(1);

      }else if(val=='z_report'){
        $('#op4div').hide();
        $('#op2div').hide(); 
      $('#op3div').show();
      get_any_report(2);

      }else if(val=='eod_report'){
         $('#op2div').hide();
         $('#op3div').hide();
        $('#op4div').show();
        get_end_report();

      }else{
        $('#op2div').hide();
         $('#op3div').hide();
        $('#op4div').hide();

      }
      
   }


   /*Remove Discount 按钮点击事件*/

  function removeAllDiscount(){

    document.getElementById("discount-webpos").style.display="none";
    document.getElementById("webpos_overlay").style.display="none";

  }

 //显示添加顾客 表单
 function showAddCustomer(){
   document.getElementById("popup-customer").style.top="-100vh";
   document.getElementById("popup-customer").style.left="35vw";
   document.getElementById("webpos_overlay").style.display="none";
   $('.bg-create-customer').show();
   $('#create-customer').show();

 }
//移除顾客  事件函数
function removeCustomer(){
  
  $('#show-customer-orders').hide();
  $('#remove-customer').hide();
  $('#add-customer').text('添加用户');
  $('#add-customer').removeClass('active');
  $('#add-customer').attr("onclick","showCustomers()");

}



//显示顾客订单

function showCustomerOrders(email){
   var orders_area_obj=document.getElementById("orders_area");
    orders_area_obj.style.background="#fff";
    orders_area_obj.style.display="block";
    orders_area_obj.style.zIndex=12;



  $('#orderlist-search-form').show();
  $('#order_list_grid').show();
  $('#orderlist_top_bar').hide();
  $('#order_detail_area').empty();

}









/*隐藏添加顾客弹窗*/
 function hidePopupCustomer(){

  
   $('#create-customer').hide(); 
  
   $('.bg-create-customer').hide();
 }

 function hidePopupCustomer(){

   $('#create-customer').hide(); 
  
   $('.bg-create-customer').hide();

 }

 function toggleAddBook(obj){


  var classVal = obj.getAttribute("class");
     if(classVal=="toggle btn btn-default off"){
       classVal = classVal.replace("toggle btn btn-default off","toggle btn btn-primary");
         obj.setAttribute("class",classVal );
     
     }else{
     
        classVal = classVal.replace("toggle btn btn-primary","toggle btn btn-default off");
          obj.setAttribute("class",classVal );
     }
 }


 function saveCustomer(){
    var first_name=$("#first_name").val(); 
    var last_name=$("#last_name").val(); 

    var email_address=$("#email_address").val(); 

    //var phone_number=$("#phone_number").val(); 

    var  province=$("#region_id").val(); 
   var i=0;
  if(first_name=="") 
  { 
   

    if($("#first_name").parent().find('div').length==0){
       $("#first_name").parent().append('<div class="validation-advice" id="advice-required-entry-first_name" style="">This is a required field.</div>');
    }
   
   
    i++;
  }else{


      if($("#first_name").parent().find('div').length==1){
          $("#first_name").parent().find('div').remove();
      }


  } 


  if(last_name=="") 
  { 
    if($("#last_name").parent().find('div').length==0){
    $("#last_name").parent().append('<div class="validation-advice" id="advice-required-entry-first_name" style="">This is a required field.</div>');
     }
     i++;
  
  } else{

    if($("#last_name").parent().find('div').length==1){
          $("#last_name").parent().find('div').remove();
      }

  }

  if(email_address=="") 
  { 
    if($("#email_address").parent().find('div').length==0){
    $("#email_address").parent().append('<div class="validation-advice" id="advice-required-entry-first_name" style="">This is a required field.</div>');
     }
     i++;
  
  } else{
      if($("#email_address").parent().find('div').length==1){
          $("#email_address").parent().find('div').remove();
      }

  }
  

   if(province=="") 
  { 
    
    if($("#region_id").parent().find('div').length==0){
    $("#region_id").after('<div class="validation-advice" id="advice-required-entry-first_name" style="">This is a required field.</div>');
     }
     i++;
  
  } else{
    if($("#region_id").parent().find('div').length==1){
          $("#region_id").parent().find('div').remove();
      }

  }
  
   if(i==0){
     //验证通过  ajax  提交表单
     var data = {};
     data.firstname = first_name;
      $.ajax({
       type:"POST",
       url:"index.php?act=add_customer",
       data:data,
       dataType:"json",
       success:function(res){ 
           $('#create-customer').hide(); 
  
           $('.bg-create-customer').hide(); 
           $('#add-customer').addClass('active');
           //显示右边两个按钮
           $('#show-customer-orders').show();
           $('#remove-customer').show();
           //改变 onclick 事件函数
            $('#add-customer').attr("onclick","showAddCustomer()");
 
           $('#add-customer').html(res);
       }
   });

    
   }




 }

 function showCustomPriceTab(){
  
  /* $('#btn-custom').removeClass('nochoose').removeClass('choose');
     $('#btn-discount').removeClass('nochoose').removeClass('choose');
     $(this).toggleClass("nochoose");
     $(this).toggleClass("choose");
      $('#edit_cart_item_popup .edit_cart_item_popup').css('height','356px');
      $('.custom_price_tab').css('right','0%');*/

   $('#btn-discount').trigger('click');




 }


function transactionClearBox(){

  $('#transaction_note').val('');

}

/*报告中count 按钮 效果*/
function showCountCashArea(){

  $('#webpos_fixed_overlay').show();
  $('#count_cash_area').css('top','10vh');

}

function hideCountCashArea(){

 //$('#webpos_fixed_overlay').hide();
 $('#count_cash_area').css('top','-100vh');

}
/*点击在线功能  效果*/
function changeOffline(obj){
  if($(obj).hasClass('nowoffline')){
     $(obj).removeClass('nowoffline');
     $(obj).addClass('nowonline');

  }else{

     $(obj).addClass('nowoffline');
     $(obj).removeClass('nowonline');
  }

  $('#bt_hold_order').show();
  $('#footer_right_overlay').css('width','70%');
  $('#bt_checkout').css('width','66%');

}

function clocse_overlay(){
  $('#holded_orders_section').hide();
  $('#cashdrawer_area').hide();
  $('#reports_area').hide();
  $('#webpos_medium_overlay').hide();

}





$(function(){
 
	 /*  $('#bt_inc_qty').on('click',function(){		  		 		
	  var val=$('#editpopup_product_qty').val();
	  val=parseInt(val);
     var val1=val+1
	  $('#editpopup_product_qty').val(val1);

    $('#webpos_cart').find('div[class$="active"]').find('div:eq(0) span').removeClass('hide');
    $('#webpos_cart').find('div[class$="active"]').find('div:eq(0) span').text(val1);
	});


	  $('#bt_desc_qty').on('click',function(){

        var val=$('#editpopup_product_qty').val();
	    val=parseInt(val);
	    if(val>0){
           var val2=val-1;
           $('#editpopup_product_qty').val(val2);
        $('#webpos_cart').find('div[class$="active"]').find('div:eq(0) span').removeClass('hide');
        $('#webpos_cart').find('div[class$="active"]').find('div:eq(0) span').text(val2);

	    }
	 
	  }); */

    /*Custom Price 和 Discount 按钮 切换效果*/



    $('#edit_cart_item_popup').delegate('#btn-custom','click',function(){
    
     $('#btn-custom').removeClass('nochoose').removeClass('choose');
     $('#btn-discount').removeClass('nochoose').removeClass('choose');
     $(this).toggleClass("nochoose");
     $(this).toggleClass("choose");
     $('#edit_cart_item_popup .edit_cart_item_popup').css('height','356px');
      $('.custom_price_tab').css('right','0%');
    });

   $('#edit_cart_item_popup').delegate('#btn-discount','click',function(){
   
     $('#btn-custom').removeClass('nochoose').removeClass('choose');
     $('#btn-discount').removeClass('nochoose').removeClass('choose');
     $(this).toggleClass("nochoose");
     $(this).toggleClass("choose");
      $('#edit_cart_item_popup .edit_cart_item_popup').css('height','356px');
      $('.custom_price_tab').css('right','0%');
    });





   //购物车中选择数量的弹窗上面的 $ 和 %  两个按钮切换效果

   $('#edit_cart_item_popup').delegate('#btn-percent','click',function(){
      $('#txtScream2').attr('placeholder','0.00%');
       $(this).siblings() .removeClass('btn2').addClass('btn1');
                    
         if($(this).hasClass("btn1")){
            
               $(this).removeClass("btn1").addClass('btn2');
         }


   });

     $('#edit_cart_item_popup').delegate('#btn-dollor','click',function(){
      $('#txtScream2').attr('placeholder','$0.00');
       $(this).siblings() .removeClass('btn2').addClass('btn1');
                    
        if($(this).hasClass("btn1")){
            
               $(this).removeClass("btn1").addClass('btn2');
         }


   });

    $('#cdexport').click(function(){
       
       if($('#transaction_export').hasClass('in')){
       
         $('#transaction_export').removeClass('in');
       }else{
        
         $('#transaction_export').addClass('in');
       }

     

    });




  $('#shipping_method li').on('click',function(){

   $('#shipping_method li').find("span[id$='selected_icon']").addClass('hide');

   $(this).find("span[id$='selected_icon']").removeClass('hide');


  });



  $('#edit_add label').on('click',function(){

     $(this).parent().parent().toggleClass('off');
     $(this).parent().parent().toggleClass('btn-default');
     $(this).parent().parent().toggleClass('btn-primary');


  });



  $('#content_general form .box1 div').on('click',function(){
   
   $('#content_general form .box1 div span').removeClass('glyphicon glyphicon-ok haschoose');
   $(this).find("span").addClass('glyphicon glyphicon-ok haschoose');

  });

  /*apply 按钮点击事件*/

    $('#discount-webpos').delegate("#btn-apply","click",function(){
	  
	  
	  
	  
	   var yhprice= $('#txtScream1').val();	
	   
	   var yhclass= $('#btn-percentx').attr('class');	
	   
	   var yhhao= $('#apply_discount_coupon_code').val();	
	   	
				
					
					$.ajax({ 
					
						url: "flow.php?act=yhprice",
						cache:false,
						data: {'yhprice':yhprice,'yhclass':yhclass,'yhhao':yhhao}, 
						dataType: 'json', 
						type: 'post', 
						
						 beforeSend:function(){
             
							   $('#ajaxload').show();
							   $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
							},
							
						
						success: function(data){
												
							$('#ajaxload').hide();
							$("#ajaxload img").remove();
									
							
							document.getElementById('qiehuan').innerHTML = data.content;
							 
							   $("#yhprice").text(data.youhui);
							   
							  // $('#webpos_subtotal_button span').text(data.zongjia);
								  
								  
							   $('#pos_totals').find('.online_totals').show();
								document.getElementById("discount-webpos").style.display="none";
								document.getElementById("webpos_overlay").style.display="none";
								$('#btn-cancel').show();
								$('#grand_total_wp_show .online_totals').hide();
								
								
								 $('#add-discount').hide();
								$('#current_dis_wp').show();
								
								
								
						}
					});
					
  });

  /*给 报告 菜单中的count 弹出层 里面的input  绑定 失去焦点事件*/
  $('#count_cash_table input[id^="count_"]').focus(function(){

     $(this).val('');


  });

 $('#count_cash_table input[id^="count_"]').blur(function(){

     var count =$(this).val();
     if(count && count>0){
         var td= $(this).parent();
         var price=td.prev().text();
         count=parseInt(count);
         price=parseInt(price);
         var total1=count*price;
         td.next().find('input').val(total1);

     }

   /*计算 合计 */

   var inputs=$('#count_cash_table tr').has('input').find('input:eq(1)');
   var p=0
   for(var i=0 ;i<inputs.length;i++){
    var inputval=parseInt(inputs[i].value);
    if(inputval && inputval>0){
         
       p+=inputval;

    }

  }
  
  $('#btn_total').text('Total'+' '+p.toFixed(2));



 });


  $('#btn_total').click(function(){

    var str=$('#btn_total').text();
    var total_price=parseInt(str.substr(6));
    total_price=total_price.toFixed(2);
    $('#cashforpos').val(total_price);

   $('#webpos_fixed_overlay').hide();
   $('#count_cash_area').css('top','-100vh');



  });




  /*验证添加顾客表单提交*/


  /*$('#form-create-customer').submit(function(){ 
    alert('ok');
    var first_name=$("#first_name").val(); 
    var last_name=$("#last_name").val(); 

    var email_address=$("#email_address").val(); 

    var phone_number=$("#phone_number").val(); 

    var address=$("#billing:region_id").val(); 

  if(first_name=="") 
  { 
   $('#first_name').insertAfter('<div class="validation-advice" style="">This is a required field.</div>');
   $("#name").focus(); 
   return false; 
  } 



  });*/




  if($('#webpos_cart > div').length > 0){
 	
      //隐藏
    
        $('#footer_right_overlay').addClass('hide');

//                 //$('#bt_hold_order').show();

//                 //$('#bt_checkout').css('width','96%');
 }else{
 	//显示
    $('#footer_right_overlay').removeClass('hide');

 }





});  //页面加载完成后

//获取x或z报告
function get_any_report(val)
{
	 $.ajax(
      { 
        url: "idx.php?act=get_x_report",
        cache:false,
        data: {'type':val},
        dataType: 'json', 
        type: 'post', 
        beforeSend:function(){
               $('#ajaxload').show();
               $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
            },
        success: function(result)
        {
          $('#ajaxload').hide();
          $("#ajaxload img").remove();
          if (result.msg == 1) {
          	document.getElementById('op2div').innerHTML = result.content;
          }else
          {
          	document.getElementById('op3div').innerHTML = result.content;
          }
        } 
      });
}
//刷新列表
function refresh_list()
{
	var type = document.getElementById('report_type').value;
	if (type == 'x_report') 
	{
		get_any_report(1);
	}
	if (type == 'z_report') 
	{
		get_any_report(2);
	}
	if (type == 'eod_report') 
	{
		get_end_report();
	}
}

function get_end_report()
{
	$.ajax(
      { 
        url: "idx.php?act=get_end_report",
        cache:false,
        dataType: 'json', 
        type: 'post', 
        beforeSend:function(){
               $('#ajaxload').show();
               $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
            },
        success: function(result)
        {
          $('#ajaxload').hide();
          $("#ajaxload img").remove();
          document.getElementById('op4div').innerHTML = result.content;
        } 
      });
}


function recently_add(val){
 	var url = "";
 	if (val == 1){
 		url = "idx.php?act=recently_add";
 	}else{
 		url = "idx.php?act=frequently_add";
 	}				
	$.ajax({ 
	
		url: url,
		cache:false,
		dataType: 'json', 
		type: 'post', 
		  beforeSend:function(){
			   $('#ajaxload').show();
			   $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
			},
		success: function(data){
				$('#ajaxload').hide();
			    $("#ajaxload img").remove();
				document.getElementById('product_content').innerHTML = data.content;
				$("#menu2").hide();
				$('#category_dropdown div a span:eq(1)').text('最近添加');
				$("#webpos_overlay").hide();
		}
	});				
}

function customeCartAdd()
{
	//商品名称,价格,配送,税类型,数量 
	var goodsName = $('#name_custom_sale').val();
	var goodsPrice = $('#txtScream').val();
	var taxType = $('#custom_sale_tax_class_id').val();
	var qty = $('#custom_sale_qty').val();
	var isShipping = $('#shipable_wp').prop("className") == "toggle btn btn-primary" ? 1 : 0;

	$.ajax({ 
		url: "idx.php?act=customer_cart",
		data: {'goodsName':goodsName,'goodsPrice':goodsPrice,'taxType':taxType,'qty':qty,'isShipping':isShipping},
		cache:false,
		dataType: 'json', 
		type: 'post', 
		beforeSend:function(){
			   $('#ajaxload').show();
			   $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
			},
		success: function(data){
				$('#ajaxload').hide();
			    $("#ajaxload img").remove();
			    //回调内容
			    $('#footer_right_overlay').addClass('hide');	
				document.getElementById('qiehuan').innerHTML = data.content;
				var id='#'+gid+'_options_popup';
				$(id).hide();
			    cancelCustomersale();
		}
	});			
}

//定制商品减
function customer_goods_plus(gid)
{
	if ($('#editpopup_product_qty'+gid).val()>1) 
	{
		customer_goods_num_edit(1);
	}			
}
//定制商品加
function customer_goods_minus(gid)
{
	if ($('#editpopup_product_qty'+gid).val()>1) 
	{
		customer_goods_num_edit(0);
	}
}
//客制化商品数据请求
function customer_goods_num_edit(val)
{
	$.ajax({ 
		url: "idx.php?act=customer_update_cart",
		data: {'type':val},
		cache:false,
		dataType: 'json', 
		type: 'post', 
		beforeSend:function(){
			   $('#ajaxload').show();
			   $("#ajaxload").append('<img src="templates/images/ajax-loader2.gif"  style=" position: fixed;top: 50%;left:50%;z-index:100; opacity: 1;"/>');
			},
		success: function(data){
				$('#ajaxload').hide();
			    $("#ajaxload img").remove();
			    //回调内容
			    $('#footer_right_overlay').addClass('hide');	
				document.getElementById('qiehuan').innerHTML = data.content;
				var id='#'+gid+'_options_popup';
				$(id).hide();
			    cancelCustomersale();
		}
	});
}